package com.rankeiot.platform.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.util.*;

@SuppressWarnings("AlibabaConstantFieldShouldBeUpperCase")
@RestController
@RequestMapping("license")
@RequiredArgsConstructor
public class LicenseController {
    public static Map<String, String> info = new HashMap<>();
    public static final long WarnTime = 10 * 24 * 60 * 60 * 1000L;
    public static String SN="";
    public static String licenseInfo =null;

    final SqlToyLazyDao dao;
    final ApplicationContext applicationContext;

    @PostMapping("detail")
    public Map<String, String> detail() {
        Map<String,String> lic=new HashMap<>();
        lic.putAll(info);
        lic.put("sn",SN);
        return lic;
    }

    @GetMapping("request")
    public String getSN() {
        return SN;
    }

    @Valid
    @PostMapping("update")
    public String update(@NotNull String password, MultipartFile file) throws Exception {
        licenseInfo=null;
        //验证管理员密码
        // UserInfo info=dao.loadBySql("select * from t_user where username=:username and enable=1 #[and password=md5(concat(username,'$',:password))]",);
        Map<String, Object> params = new HashMap<>();
        params.put("password", password);
        Object ret = dao.getSingleValue("select username from t_user where username='admin' and password=md5(concat(username,'$',:password))", params);
        if (ret == null) {
            return "密码错误";
        }
        //UploadedFile uf = ctx.file("file");
        if (file == null) {
            return "上传文件为空";
        }
        //验证
        byte[] data = file.getBytes();//Utils.transferToBytes(uf.getContent());
        //uf.getContent().close();
        if(data==null){
            return "数据错误";
        }
        //applicationContext.publishEvent(new ApplicationEvent("updateLicense",data));
        //EventBus.push(new ApplicationEvent("updateLicense",data));
        return licenseInfo;
    }

}
